package middleware

import (
	"fmt"
	"k8s-admin/dao/system/casbin"
	"k8s-admin/utils"

	"github.com/gin-gonic/gin"
)

func CasbinMiddle() gin.HandlerFunc {
	return func(c *gin.Context) {

		uid := c.GetString("userId")
		username := c.GetString("username")
		fmt.Printf("username: %v\n", username)
		if username == "admin" {
			c.Next()
			return
		}
		ok := casbin.AuthCheck(uid, c.Request.Method, c.Request.RequestURI)
		if !ok {
			utils.ReturnContext(c).Failed("失败", "没有访问权限")
			c.Abort()
			return
		} else {
			c.Next()
		}

	}
}
